//
//  BLToBeWashingViewController.m
//  blanket
//
//  Created by Mac on 2017/12/5.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLCustomViewController.h"

@interface BLCustomViewController ()
@property(nonatomic, strong) BLTableControl                     *contactControl;
@property(nonatomic, strong) BLTableControl                     *phoneControl;
@property(nonatomic, strong) BLTableControl                     *badControl;
@property(nonatomic, strong) BLTableControl                     *nameControl;
@property(nonatomic, strong) BLTableControl                     *addControl;
@end

@implementation BLCustomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请定制业务";
    [self configSubView];
    [self configTableFooter];
    [self.tableView st_autoAdjustAllResponder];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 122)];
    header.backgroundColor = self.tableView.backgroundColor;
    
    self.contactControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20, UIScreenWidth - 30, 50) leftTitle:@"联系人" placeHolder:@"请输入姓名"];
    [header addSubview:self.contactControl];
    
    self.phoneControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.contactControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"联系电话" placeHolder:@"请输入电话号码"];
    self.phoneControl.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:self.phoneControl];
    
    self.badControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.phoneControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"酒店规模" placeHolder:@"请输入酒店的床位"];
    self.badControl.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:self.badControl];
    
    self.nameControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.badControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"酒店名称" placeHolder:@"请输入酒店名称"];
    [header addSubview:self.nameControl];
    
    self.addControl = [[BLTableControl alloc] initWithFrame:CGRectMake(15, 20 + self.nameControl.bottom, UIScreenWidth - 30, 50) leftTitle:@"酒店地址" placeHolder:@"请输入酒店地址"];
    [header addSubview:self.addControl];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(20, self.addControl.bottom + 20, UIScreenWidth - 40, 20)
                                                     text:@"提示：请输入真实的酒店信息，以便我们联系您"
                                                textColor:BL_BlueBackGroundColor
                                                     font:12
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [header addSubview:titleLable];
    
    header.height = titleLable.bottom + 20;
    self.tableView.tableHeaderView = header;
    
    
}
- (void)configTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"提交"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    if (!self.contactControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入联系人"];
        return;
    }
    if (!self.phoneControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入联系电话"];
        return;
    }
    if (!self.badControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入酒店规模"];
        return;
    }
    if (!self.nameControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入酒店名称"];
        return;
    }
    if (!self.addControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入酒店地址"];
        return;
    }
    [self sendToBeWashingRequest];
}
- (void)sendToBeWashingRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/applyService"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (self.contactControl.textFiled.text.length) {
        [dic setObject:self.contactControl.textFiled.text forKey:@"name"];
    }
    if (self.phoneControl.textFiled.text.length) {
        [dic setObject:self.phoneControl.textFiled.text forKey:@"mobile"];
    }
    if (self.badControl.textFiled.text.length) {
        [dic setObject:self.badControl.textFiled.text forKey:@"hotel_scale"];
    }
    if (self.addControl.textFiled.text.length) {
        [dic setObject:self.addControl.textFiled.text forKey:@"hotel_address"];
    }
    if (self.nameControl.textFiled.text.length) {
        [dic setObject:self.nameControl.textFiled.text forKey:@"hotel_name"];
    }
    
    [dic setObject:@"2" forKey:@"type"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"已经收到你的定制申请"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

